using System;
using Microsoft.SPOT;
using Netduino.Sensor.Json;
using Netduino.Sensor.Unit;

namespace Netduino.Sensor
{
    /// <summary>
    /// An implementation of a sample.
    /// </summary>
    internal class Sample : ISample, IJsonSerializable
    {
        /// <summary>
        /// Implements a JSON serializer
        /// </summary>
        /// <returns>A JSON representation of this.</returns>
        public String ToJson()
        {
            return
                @"{""sample"": {" +
                @"""value"": """ + this.Value.ToString("F") + @"""," +
                @"""unitname"": """ + this.Unit.Name + @"""," +
                @"""unitsymbol"": """ + this.Unit.Symbol + @"""" +
                "}}";
        }

        /// <summary>
        /// Backing field.
        /// </summary>
        private double _value;

        /// <summary>
        /// Creates a new instance with the specified value in the specified unit.
        /// </summary>
        /// <param name="value">The value of the sample.</param>
        /// <param name="unit">The unit of the value</param>
        public Sample(double value, IUnit unit)
        {
            Value = value;
            Unit = unit;
        }

        /// <summary>
        /// The value of the sample
        /// </summary>
        public virtual double Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                Time = DateTime.Now; //sample was supposedly taken now
            }
        }

        /// <summary>
        /// The unit of the sample's value
        /// </summary>
        public virtual IUnit Unit { get; set; }


        /// <summary>
        /// The time at which this was taken.
        /// </summary>
        public DateTime Time { get; private set; }
    }
}
